import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import filters from './common/js/filters.js'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'

// import Mock from './mock'
// Mock.bootstrap();

import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
Vue.prototype.$axios = axios
import VueAxios from 'vue-axios'

Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
})
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueAxios, axios)


const router = new VueRouter({
  mode: 'history',
  routes
})




// 设置全局axios请求头

axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('Authorization')) {
      config.headers.Authorization = localStorage.getItem('Authorization');
    };
    return config
  },
  err => {
    return Promise.reject(err)
  })

axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    console.log('error');
    if (error.response) {
      switch (error.response.status) {
        case 401:
        console.log('token过期');
          // 返回401的时候 清楚token信息
          // store.commit('logout');
          localStorage.removeItem('Authorization')
          router.replace({
            path: '/login',
            query: { redirect: router.currentRoute.fullPath }
          })
      }
    }
    return Promise.reject(error.response)   // 返回接口返回的错误信息
  });
//NProgress.configure({ showSpinner: false });



router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    localStorage.removeItem('Authorization');
  }
  let user = localStorage.getItem('Authorization');
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

